<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

<head th:include="include :: header"></head>
<body class="gray-bg">

<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <input type="hidden" name="role" id="role" th:value="${role}"/>
            <form id="formId">
                <!--<input type="hidden" name="dateNow" id="dateNow"
                       th:value="${#dates.format(new java.util.Date().getTime(), 'yyyy-MM-dd')}"/>-->
                <div class="select-list">
                    <ul>
                        <li>商户编号：<input type="text" name="merchantNo"/>
                        </li>

                        <li>商户名称：<input type="text" name="merchantCnName"/>
                        </li>
                        <li>客户账号：<input type="text" name="phone"/>
                        </li>
                        <li>联系电话：<input type="text" name="reservePhone"/>
                        </li>


                        <li>身份证号：<input type="text" name="idCardNumber"/>
                        </li>
                        <li>银行卡号：<input type="text" name="bankAccount"/>

                        </li>

                        <li class="select-time"><label>创建时间： </label> <input
                                type="text" class="time-input" id="beginTime1"
                                placeholder="开始时间" name="params[beginTime1]"/> <span>-</span>
                            <input type="text" class="time-input" id="endTime1"
                                   placeholder="结束时间" name="params[endTime1]"/></li>

                      <!--  <li class="select-time"><label>升级时间： </label> <input
                                type="text" class="time-input" id="beginLastUpdateTime"
                                placeholder="开始时间" name="params[beginLastUpdateTime]"/> <span>-</span>
                            <input type="text" class="time-input" id="endTimeLastUpdateTime"
                                   placeholder="结束时间" name="params[endTimeLastUpdateTime]"/></li>
-->
                        <li>认证状态：<select name="params[realNameStatus]"
                                         th:with="type=${@dict.getType('sys_freeze_status')}">
                            <option value="">所有</option>
                            <option value="10B">已认证</option>
                            <option value="10A">未认证</option>
                            <option value="10C">实名失败</option>
                        </select>
                        </li>

                        <li>商户等级：<select name="level"
                                         th:with="type=${@dict.getType('sys_merchant_level')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>

                        <li th:if="${role}!=agent">员工状态：<select name="isCompany"
                                                                th:with="type=${@dict.getType('sys_merchant_is_company')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li><a class="btn btn-primary btn-rounded btn-sm"
                               onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a> <a
                                class="btn btn-warning btn-rounded btn-sm"
                                onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>

                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
            </a> <a class="btn btn-warning" onclick="$.table.exportExcel()"
                    shiro:hasPermission="system:merchant:export"> <i
                class="fa fa-download"></i> 导出
        </a>

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>

</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:merchant:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:merchant:remove')}]];
    var listFlag = [[${@permission.hasPermi('system:merchant:list')}]];
    var prefix = ctx + "system/merchant";

    var datas = [[${@dict.getType('sys_merchant_level')}]];//商户等级
    var datas1 = [[${@dict.getType('sys_freeze_status')}]];//商户实名认证状态
    var datas2 = [[${@dict.getType('sys_status')}]];//用户状态
    var datas4 = [[${@dict.getType('sys_merchant_is_company')}]];//是否公司员工
    $(function () {
        var role = $("#role").val();
        if (role == "admin" || role == "cs") {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                updateRoleUrl: prefix + "/editRole/{id}", //修改权限
                updateQuotaUrl: prefix + "/editQuota/{id}",//修改限额
                modalName: "商户",
                queryParams: queryParams,
                sortName: " createTime",
                sortOrder: "desc",
                search: false,
                showExport: true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'id',
                        title: 'id',
                        visible: false
                    },
                    {
                        field: 'merchantNo',
                        align: 'center',
                        title: '商户编号',
                        sortable: true,
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return '<a href="#" onclick="detail(\'' + row.id + '\')">' + value + '</a>';
                            } else {
                                return value;
                            }
                        }
                    },
                    {
                        field: 'merchantCnName',
                        align: 'center',
                        title: '商户名称',
                        sortable: true
                    },

                    {
                        field: 'parentMerchantName',
                        title: '上级姓名',
                    },
                    {
                        field: 'parentMerchantPhone',
                        title: '上级联系电话',
                    },
                    {
                        field: 'level',
                        title: '商户等级',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas, value);
                        }
                    },
                    {
                        field: 'phone',
                        title: '客户账号',

                    },
                    {
                        field: 'reservePhone',
                        title: '联系电话',
                        formatter: function (value, row, index) {
                            return "<strong style='color: mediumslateblue's>" + value + "</strong>";
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                    },
                    // {
                    //     title: '钱包余额',
                    //     align: 'center',
                    //     formatter: function (value, row, index) {
                    //         var actions = [];
                    //         var role = $("#role").val()
                    //         if (role == "admin" || role == "cs") {
                    //
                    //                 var str = row.settleStatus;
                    //                 actions.push('<a href="#" onclick="findMoneyCj(\'' + row.id + '\')">' + '钱包余额</a>');
                    //         }
                    //         return actions.join('');
                    //     }
                    // },
                    {
                        field: 'idCardNumber',
                        title: '身份证号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return value;
                            } else {
                                return subs(value)
                            }
                        }
                    },
                    {
                        field: 'bankName',
                        title: '开户行',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            if (value != null) {
                                return "<strong style='color: #1E0FBE's>" + value + "</strong>";
                            }

                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '银行卡号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return value;
                            } else {
                                return subs(value)
                            }

                        }
                    },
                    {
                        field: 'realNameStatus',
                        title: '实名状态',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas1, value);
                        }
                    },
                    {
                        field: 'userStatus',
                        title: '用户状态',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas2, value);
                        }
                    },


                    {
                        field: 'isCompany',
                        title: '是否员工',
                        align: 'center',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas4, value);
                        }
                    },
                    {
                        title: '客户关系',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="#" onclick="findMerchantParent(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>修改上下级 </a> <br>');

                            actions.push('<a class="btn btn-info btn-xs ' + listFlag + '" href="#" onclick="customerRelation(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>客户关系</a> ');
                            if (row.level == "6") {
                            } else {
                                actions.push('<a class="btn btn-success btn-xs ' + listFlag + '" href="#" onclick="superiorRelation(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>上级关系</a> ');
                            }
                            return actions.join('');
                        }
                    },
                    {
                        title: '设置',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            var role = $("#role").val()
                            if (role == "admin" || role == "cs") {
                                if (row.userStatus == "10A" || row.parentPhone != '123') {
                                    if (row.realNameStatus == "10B"||row.realNameStatus == "10E") {
                                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="#" onclick="$.operate.updateRoleUrl(\'' + row.id + '\')"><i class="fa fa-edit"></i>修改等级</a> ');
                                    }
                                }
                                actions.push('<a class="btn btn-success btn-xs ' + listFlag + '" href="#" onclick="resetPwd(\'' + row.id + '\')"><i class="fa fa-key"></i>重置支付密码</a>');
                            }
                            return actions.join('');
                        }
                    },
                    {
                        title: '详情',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-info btn-xs ' + '" href="#" onclick="merchantUpgrade(\'' + row.id + '\')"><i class="fa fa-list-ul"></i>升级详情</a> ');
                            if (row.realNameStatus == "10B"||row.realNameStatus == "10E") {
                                actions.push('<a class="btn btn-success btn-xs ' + '" href="#" onclick="findNumber(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>客户关系数量</a> ');
                            }
                            return actions.join('');
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            var role = $("#role").val()
                            if (role == "admin" || role == "cs") {
                                actions.push(statusTools(row));
                            }
                            return actions.join('');
                        }
                    },
                ]
            };
            $.table.init(options);
        } else {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                updateRoleUrl: prefix + "/editRole/{id}", //修改权限
                updateQuotaUrl: prefix + "/editQuota/{id}",//修改限额
                modalName: "商户",
                queryParams: queryParams,
                sortName: " createTime",
                sortOrder: "desc",
                search: false,
                showExport: true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'id',
                        title: 'id',
                        visible: false
                    },
                    {
                        field: 'merchantNo',
                        align: 'center',
                        title: '商户编号',
                        sortable: true,
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return '<a href="#" onclick="detail(\'' + row.id + '\')">' + value + '</a>';
                            } else {
                                return value;
                            }
                        }
                    },
                    {
                        field: 'merchantCnName',
                        align: 'center',
                        title: '商户名称',
                        sortable: true
                    },
                    {
                        field: 'parentMerchantName',
                        title: '上级姓名',
                    },
                    {
                        field: 'parentMerchantPhone',
                        title: '上级联系电话',
                    },
                    {
                        field: 'level',
                        title: '商户等级',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas, value);
                        }
                    },
                    {
                        field: 'phone',
                        title: '客户账号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            return "<strong style='color: mediumslateblue's>" + value + "</strong>";
                        }
                    },
                    {
                        field: 'reservePhone',
                        title: '联系电话',
                        formatter: function (value, row, index) {
                            return "<strong style='color: mediumslateblue's>" + value + "</strong>";
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                    },
                    {
                        field: 'idCardNumber',
                        title: '身份证号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return value;
                            } else {
                                return subs(value)
                            }
                        }
                    },
                    {
                        field: 'bankName',
                        title: '开户行',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            if (value != null) {
                                return "<strong style='color: #1E0FBE's>" + value + "</strong>";
                            }

                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '银行卡号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var role = $("#role").val();
                            if (role == "admin" || role == "cs") {
                                return value;
                            } else {
                                return subs(value)
                            }

                        }
                    },
                    {
                        field: 'realNameStatus',
                        title: '实名状态',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas1, value);
                        }
                    },
                    {
                        field: 'userStatus',
                        title: '用户状态',
                        align: 'center',
                        sortable: true,
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(datas2, value);
                        }
                    },
                    {
                        title: '客户关系',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-info btn-xs ' + listFlag + '" href="#" onclick="customerRelation(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>客户关系</a> ');
                            actions.push('<a class="btn btn-success btn-xs' + listFlag + '" href="#" onclick="superiorRelation(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>上级关系</a> ');

                            return actions.join('');
                        }
                    },
                    {
                        title: '详情',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-info btn-xs ' + '" href="#" onclick="merchantUpgrade(\'' + row.id + '\')"><i class="fa fa-list-ul"></i>升级详情</a> ');
                            if (row.realNameStatus == "10B"||row.realNameStatus == "10E") {
                                actions.push('<a class="btn btn-success btn-xs ' + '" href="#" onclick="findNumber(\'' + row.phone + '\')"><i class="fa fa-list-ul"></i>客户关系数量</a> ');
                            }
                            return actions.join('');
                        }
                    },
                ]
            };
            $.table.init(options);
        }
    });

    function queryParams(params) {

        return {
            "params[beginTime1]": $("#beginTime1").val(),
            "params[endTime1]": $("#endTime1").val(),
            "params[beginLastUpdateTime]": $("#beginLastUpdateTime").val(),
            "params[endTimeLastUpdateTime]": $("#endTimeLastUpdateTime").val(),
            pageSize: params.limit,
            pageNum: params.offset / params.limit + 1,
            searchValue: params.search,
            orderByColumn: params.sort,
            isAsc: params.order
        };
    }


    //详情	detailUrl: prefix + "//{id}",
    function detail(id) {
        var url = prefix + '/detail/' + id;
        createMenuItem(url, "查看详情");
    }

    //使用状态
    function statusTools(row) {
        var content = "";
        if (row.userStatus == "10A") {
            content += '<a class="btn btn-danger btn-xs' + removeFlag + '" href="#" onclick="prohibit(\'' + row.id + '\')")" ><i class="fa fa-remove"></i>禁用</a> ';
        } else {
            content += '<a class="btn btn-info btn-xs" href="#" onclick="enable(\'' + row.id + '\')"><i class="fa fa-play"></i>启用</a> ';
        }
        return content;
    }


    /*禁用*/
    function prohibit(id) {
        $.modal.confirm("确认要禁用该商户吗？", function () {
            $.operate.post(prefix + "/updateUseStatusById", {"id": id});
        })
    }

    // 启用
    function enable(id) {
        $.modal.confirm("确认要启用该商户吗？", function () {
            $.operate.post(prefix + "/updateUseStatusById", {"id": id});
        })
    }

    //客户关系
    function customerRelation(phone) {
        var url = prefix + '/customerRelation/' + phone;
        createMenuItem(url, "客户关系");
    }

    //上级关系
    function superiorRelation(phone) {
        var url = prefix + '/superiorRelation/' + phone;
        createMenuItem(url, "上级关系");
    }

    //重置支付密码
    function resetPwd(id) {
        $.modal.confirm("确认要重置支付密码吗？", function () {
            $.operate.post(prefix + "/updatePayPwd", {"id": id});
        })
    }


    //客户关系总数量查询
    function findNumber(phone) {
        var url = prefix + '/findNumber/' + phone;
        layer.open({
            type: 2,
            area: [1000 + 'px', 300 + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "客户关系总数量查询",
            content: url,
            // 弹层外区域关闭
            shadeClose: true,
            yes: function (index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler();
            },
            cancel: function (index) {
                return true;
            }
        });
    }

    //钱包余额查询
    function findMoneyCj(id) {
        var url = prefix + '/findMoneyCj/' + id;
        layer.open({
            type: 2,
            area: [800 + 'px', 500 + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "余额查询【钱包】",
            content: url,
            // 弹层外区域关闭
            shadeClose: true,
            yes: function (index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler();
            },
            cancel: function (index) {
                return true;
            }
        });
    }

    function merchantUpgrade(id) {
        var url = prefix + '/merchantUpgrade/' + id;
        createMenuItem(url, "升级详情");
    }

    function subs(value) {
        if (value == null || value == "") {
            return "";
        } else {
            var r1 = value.substring(0, 4)
            var r2 = value.substring(value.length - 4, value.length)
            return r1 + "**** ****" + r2;
        }
    }

    //修改上下级
    function findMerchantParent(phone) {
        var url = prefix + '/findMerchantParent/' + phone;
        $.modal.open("客户关系-修改上下级", url);
    }
</script>
</body>
</html>